(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:chooseForm
     * @description
     * # chooseForm
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.wenjuan.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('chooseForm', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'EA',
                template: "<a href='javascript:;' class='sui-btn btn-link'  ng-click='showPro()'>+ {{'选择调查问卷'|translate}}",
                scope: {
                    ngModel: '=',
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', '$timeout', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache, $timeout) {

                    $scope.showPro = function() {
                        $scope.initData().then(showSelect)
                    }
                    $scope.initData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        $scope.page_size = 10;
                        var query = {
                            "store_id": $rootScope.store_id,
                            "type": "diaochawenjuan",
                        };
                        return dataService.customformGet(query).then(function(r) {
                            if (!r.Msg) {
                                $scope.form_list = r.customform_list;
                                $scope.total_count = r.total_count;
                                if ($scope.chosedForms) {
                                    if ($scope.chosedForms.length > 0) {
                                        $scope.forms = $scope.chosedForms[0];
                                    }
                                }
                            }
                        });
                    }

                    function showSelect() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"自定义表单"|translate}}',
                            body: $templateCache.get("views/directives.wenjuan.html"),
                            okBtn: '{{"确认"|translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height: 300,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $scope.okHide();
                            },
                            shown: function(e) {}
                        });
                    }

                    $scope.okHide = function() {
                        if ($scope.id) {
                            $timeout(function() {
                                $scope.ngModel = $scope.id;
                                console.error("$scope.id", $scope.ngModel);
                            });
                        }
                    }

                    $scope.isChecked = function(id) {
                        if (id) {
                            $scope.id = id;
                            console.error("$scope.id", $scope.id);
                        }
                    }
                }],
                link: function(scope, elem, attrs, ctr) {}
            }
        }])
        .directive('customformInfo', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'EA',
                template: "<span> <span ng-show='type'>{{form_name}}</span> <span ng-show='!type'>{{form_msg}}</span></span>",
                scope: {
                    ngModel: '=',
                    type: "@"
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', '$timeout', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache, $timeout) {
                    $scope.initData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        $scope.page_size = 10;
                        var query = {
                            "store_id": $rootScope.store_id,
                            "form_id": $scope.ngModel
                        };
                        return dataService.customformGet(query).then(function(r) {
                            if (!r.Msg) {
                                if (r.customform_list[0]) {
                                    $scope.form_name = r.customform_list[0].form_name;
                                    $scope.form_msg = r.customform_list[0].form_msg

                                }
                            }
                        });
                    }
                    $scope.initData()
                }],
                link: function(scope, elem, attrs, ctr) {}
            }
        }]);
})();